package com.example.apiproject.service.impl;

import com.example.apiproject.entity.Groups;
import com.example.apiproject.dao.GroupsDao;
import com.example.apiproject.service.GroupsService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Groups)表服务实现类
 *
 * @author makejava
 * @since 2023-09-23 13:40:57
 */
@Service("groupsService")
public class GroupsServiceImpl implements GroupsService {
    @Resource
    private GroupsDao groupsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Groups queryById(Integer id) {
        return this.groupsDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param groups 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Groups> queryByPage(Groups groups, PageRequest pageRequest) {
        long total = this.groupsDao.count(groups);
        return new PageImpl<>(this.groupsDao.queryAllByLimit(groups, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param groups 实例对象
     * @return 实例对象
     */
    @Override
    public Groups insert(Groups groups) {
        this.groupsDao.insert(groups);
        return groups;
    }

    /**
     * 修改数据
     *
     * @param groups 实例对象
     * @return 实例对象
     */
    @Override
    public Groups update(Groups groups) {
        this.groupsDao.update(groups);
        return this.queryById(groups.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.groupsDao.deleteById(id) > 0;
    }

    @Override
    public List<Groups> queryByUserId(String userId) {
        return this.groupsDao.queryByUserId(userId);
    }
}
